package org.example.work.controller;

import com.alibaba.fastjson2.JSON;
import jakarta.annotation.Resource;
import org.example.work.pojo.LoginLog;
import org.example.work.pojo.OperLog;
import org.example.work.pojo.Result;
import org.example.work.service.ILoginLogService;
import org.example.work.service.IOperLogService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 浦慧慧
 * @version 1.0
 * @since 2024/12/15 11.34
 */
@RestController
@RequestMapping("/logs")
public class LogController {
    public static final String LOGIN_LOG = "登录日志";
    @Resource
    private ILoginLogService loginLogService;

    @Resource
    private IOperLogService operLogService;

    /**
     * 获取日志列表
     *
     * @param type 日志类型，登录日志或操作日志
     * @return 日志列表 Result
     */
    @GetMapping
    public Result<?> getLogList(@RequestParam String type) {
        // 处理逻辑，根据 type 获取日志列表
        if (LOGIN_LOG.equals(type)) {
            List<LoginLog> list = loginLogService.list();
            return Result.success(list);
        } else {
            List<OperLog> list = operLogService.list();
            return Result.success(list);
        }
    }

    @PostMapping("/insertLoginLog")
    public Map<String,Object> insertLoginLog(@RequestBody LoginLog log) {
        System.out.println(JSON.toJSONString(log));
        loginLogService.insert(log);
        return new HashMap<>();
    }

    @PostMapping("/insertOperLog")
    public Map<String,Object> insertOperLog(@RequestBody OperLog log) {
        System.out.println(JSON.toJSONString(log));
        operLogService.insert(log);
        return new HashMap<>();
    }
}